package com.hncu.edu.info.manager.service;

import com.hncu.edu.info.manager.dao.OtherStudentDao;
import com.hncu.edu.info.manager.dao.StudentDao;
import com.hncu.edu.info.manager.domain.Student;

public class StudentService {
    OtherStudentDao studentDao = new OtherStudentDao();

    // 学号是否存在
    public boolean isExists(String sid) {
        //创建变量flag,假设学号不在数组中
        boolean flag = false;
        //获得学生数组
        Student[] arr = studentDao.findAllStudent();
        //遍历数组,判断数组中是否有相同学号
        for (int i = 0; i < arr.length; i++) {
            Student student = arr[i];
            if (student != null && student.getid().equals(sid)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //添加学生
    public boolean addStudent(Student stu) {
        //传递学生对象
        boolean result = studentDao.addStudent(stu);
        //返回布尔值给上一层
        return result;
    }

    // 查找学生
    public Student[] findAllStudent() {
        //接收数组
        Student[] arr = studentDao.findAllStudent();
        //判断返回值时有用，假定数组为空
        boolean flag = false;
        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            //如果数组内不为空
            if (stu != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            //为真返回数组
            return arr;
        } else {
            return null;
        }
    }

    //删除学生
    public void deleteStudentBySid(String deleteid) {
        //传学号
        studentDao.deleteStudentBySid(deleteid);
    }

    // 修改学生
    public void updateStudent(Student stu) {
        //传递对象
        studentDao.updateStudent(stu);
    }
}
